package com.sttk.secreconeclick.bookkeep;

import android.app.Activity;
import android.app.Application;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.sztle.admodule.outstatus.AppParam;
import com.sztle.admodule.outstatus.AppParamBuilder;
import com.sztle.admodule.outstatus.AppStatusProcessor;

public class BaseApp extends Application {

    private static boolean isBackground = false;
    private static int activityCount = 0;
    // 添加标识，用于判断是否正在进行图片选择
    public static boolean isImagePickerActive = false;

    @Override
    public void onCreate() {
        super.onCreate();

        //todo list 初始化并传入渠道、各个广告id及其他所需id
        AppParam appParam = new AppParamBuilder()
                .setChannel(BuildConfig.FLAVOR)//商店渠道
                .setDebug(BuildConfig.DEBUG) // 是否是debug模式,正式发布一定是要false
                .setGremoreAppId("5752215")// gremore聚合appid
                .setUMengAppKey("69031e0a8560e34872d9ce3d") // 友盟appkey
                .setAdsparkAppId("856570")// AdSpark的appid
                .setSplashAdId("103701717")// 开屏广告id
                .setInterstitialAdId("103701350")// 插屏广告id
                .setRewardedVideoAdId("103701528")// 激励视频广告id
                .setNativeAdId("103701088")// 原生广告id
                .build();
        AppStatusProcessor.statusStartApp(this, appParam);

        registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState) {

            }

            @Override
            public void onActivityStarted(@NonNull Activity activity) {
                activityCount++;
                // 如果正在进行图片选择，则不跳转到启动页
                if (isBackground && !(activity instanceof SplashActivity) && !isImagePickerActive) {
                    Intent intent = new Intent(activity, SplashActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    intent.putExtra("isFromAwake", true);
                    activity.startActivity(intent);
                    activity.overridePendingTransition(0, 0);
                }
                // 重置图片选择标识
                if (isImagePickerActive) {
                    isImagePickerActive = false;
                }
                isBackground = false;
                AppStatusProcessor.setBackground(false);
            }

            @Override
            public void onActivityResumed(@NonNull Activity activity) {
                if (activity instanceof BaseNoModelActivity) {
                    AppStatusProcessor.setTopActivity(activity);
                }
            }

            @Override
            public void onActivityPaused(@NonNull Activity activity) {

            }

            @Override
            public void onActivityStopped(@NonNull Activity activity) {
                activityCount--;
                if (activityCount <= 0) {
                    isBackground = true;
                    activityCount = 0;
                    AppStatusProcessor.setBackground(true);
                }
            }

            @Override
            public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle outState) {

            }

            @Override
            public void onActivityDestroyed(@NonNull Activity activity) {

            }
        });
    }
}
